package com.ruoyi.sales.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sales.domain.bo.SalesRecordBo;
import com.ruoyi.sales.domain.vo.SalesRecordVo;
import com.ruoyi.sales.domain.SalesRecord;
import com.ruoyi.sales.mapper.SalesRecordMapper;
import com.ruoyi.sales.service.ISalesRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 售后服务记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-30
 */
@RequiredArgsConstructor
@Service
public class SalesRecordServiceImpl implements ISalesRecordService {

    private final SalesRecordMapper baseMapper;

    /**
     * 查询售后服务记录
     */
    @Override
    public SalesRecordVo queryById(Long saleId){
        return baseMapper.selectVoById(saleId);
    }

    /**
     * 查询售后服务记录列表
     */
    @Override
    public TableDataInfo<SalesRecordVo> queryPageList(SalesRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SalesRecord> lqw = buildQueryWrapper(bo);
        Page<SalesRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询售后服务记录列表
     */
    @Override
    public List<SalesRecordVo> queryList(SalesRecordBo bo) {
        LambdaQueryWrapper<SalesRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SalesRecord> buildQueryWrapper(SalesRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SalesRecord> lqw = Wrappers.lambdaQuery();
        lqw.ge(bo.getServiceStartDate() != null, SalesRecord::getServiceStartDate, bo.getServiceStartDate());
        lqw.le(bo.getServiceEndDate() != null, SalesRecord::getServiceEndDate, bo.getServiceEndDate());
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), SalesRecord::getUsername, bo.getUsername());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), SalesRecord::getProductName, bo.getProductName());
        lqw.like(StringUtils.isNotBlank(bo.getProductModel()), SalesRecord::getProductModel, bo.getProductModel());
        lqw.like(StringUtils.isNotBlank(bo.getProductNumber()), SalesRecord::getProductNumber, bo.getProductNumber());
        lqw.like(StringUtils.isNotBlank(bo.getServiceStaff()), SalesRecord::getServiceStaff, bo.getServiceStaff());
        lqw.like(StringUtils.isNotBlank(bo.getSalesperson()), SalesRecord::getSalesperson, bo.getSalesperson());
        return lqw;
    }

    /**
     * 新增售后服务记录
     */
    @Override
    public Boolean insertByBo(SalesRecordBo bo) {
        SalesRecord add = BeanUtil.toBean(bo, SalesRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSaleId(add.getSaleId());
        }
        return flag;
    }

    /**
     * 修改售后服务记录
     */
    @Override
    public Boolean updateByBo(SalesRecordBo bo) {
        SalesRecord update = BeanUtil.toBean(bo, SalesRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SalesRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除售后服务记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
